package com.example.finance_oa.dubbo;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.comment_oa.pojos.Budget;
import com.example.finance_oa.mapper.BudgetMapper;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 读取excel监听器
 * @author cjl
 */
@Component
public class AccountListener extends AnalysisEventListener{
    /**
     * 没读完一行内容，调用的方法
     * @param
     * @param analysisContext
     */
    private static final int BATCH_COUNT = 100;

    private BaseMapper budgetMapper;

    public AccountListener(BudgetMapper budgetMapper){
        this.budgetMapper = budgetMapper;
    }

    List<Budget> list = new ArrayList<Budget>();

    @Override
    public void invoke(Object o, AnalysisContext analysisContext) {
        list.add((Budget) o);
        System.out.println(list);
    }

    /**
     * 读取完整个文档后调用的方法
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
    }
    private void saveData(){
        for (Budget dept : list) {
            System.out.println(dept);
            budgetMapper.insert(dept);
        }
    }
}
